package com.swpu.project.service.impl;


import com.swpu.project.mapper.MenuMapper;
import com.swpu.project.model.entity.LoginUser;
import com.swpu.project.model.entity.User;
import com.swpu.project.service.IUserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserDetailServiceImpl  implements UserDetailsService {
    @Resource
    IUserService iUserService;
    @Resource
    MenuMapper menuMapper;
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        User user = iUserService.lambdaQuery().eq(User::getUserName, userName).one();
              if(user==null)
              {
                 throw  new RuntimeException("账户或密码错误");
              }

        //TODO 封装权限到loginuser
//        List<String> list = Arrays.asList("test");
        List<String> list = menuMapper.selectPermsByUserId(user.getId());
        return new LoginUser(user,list);

    }




}
